import axios from 'axios'
import store from '../store'

const service = axios.create({
  baseURL: '',
  method: '',
  dataType: 'json',
  timeout: 10000, // request timeout
  responseType: 'json'
})

service.interceptors.request.use(
  (config) => {
    if (store.getters.token) {
      config.headers.token = store.getters.token
    }
    return config
  },
  (error) => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response) => {
    const res = response.data
    if (res.code !== 200) {
      return Promise.reject(new Error(res.message || 'Error'))
    }
    return res
  },
  (error) => Promise.reject(error)
)

export default service
